/*
 * @Author: cai.qi 
 * @Date: 2020-01-07 16:27:10 
 * @Last Modified by: cai.qi
 * @Last Modified time: 2020-05-14 16:36:05
 */


"use strict";
import { Controller } from "egg";

class TypeController extends Controller {
    docTransfer: {
        errotType: { type: string; required: boolean };
        errorInfo: { type: string; required: boolean };
        fromUrl: { type: string; required: boolean };
    };
    constructor(ctx) {
        super(ctx);
        this.docTransfer = {
            errotType: { type: "string", required: true },
            fromUrl: { type: "string", required: true },
            errorInfo: { type: "object", required: true }
        };
    }
    // ========================================  获取地址
    async get() {
        const { ctx } = this;
        console.log(ctx.request.header.openid)
        const openid = ctx.request.header.openid
        const data = await ctx.service.sevAddress.get(openid);
        ctx.helper.success({ data });
    }
    async detail() {
        const { ctx } = this;
        const { id } = ctx.query;
        console.log(id)
        const data = await ctx.service.sevAddress.findOne(id);
        ctx.helper.success({ data });
    }
    // ========================================  新增/修改地址
    async post() {
        const { ctx } = this;
        const body = ctx.request.body;
        const id = ctx.params.id
        // ctx.validate(this.docTransfer, body);
        const openid = ctx.request.header.openid
        let data = {}
        body.openid = openid
        body.is_default = String(body.isDefault) === 'true' ? 1 : 0
        body.address = `${body.province}-${body.city}-${body.county}-${body.addressDetail}`
        if (String(id) === '0') {
            data = await ctx.service.sevAddress.create(body);
        } else {
            data = await ctx.service.sevAddress.updata(id, body);
        }
        ctx.helper.success({ data });
    }
    // ========================================  新增/修改地址
    async del() {
        const { ctx } = this;
        const id = ctx.params.id
        let data = {}
        data = await ctx.service.sevAddress.del(id);
        ctx.helper.success({ data });
    }
}

export default TypeController;
